#include<bits/stdc++.h>
using namespace std;
long long jc[20];
long long C(int n,int m){
    return jc[n]/jc[m]/jc[n - m];
}
typedef vector<vector<int>> table;
const int MOD = 1e9+7;
int k1,k2;
table operator*(table va,table vb){
    table res = vector<vector<int>>(k1 + 1,vector<int>(k2 + 1));
    for(int i = 0;i <= k1;i++){
        for(int j = 0;j <= k2;j++){
            for(int p1 = 0;p1 <= i;p1++){
                for(int p2 = 0;p2 <= j;p2++){
                    res[i][j] = (res[i][j] + 1ll*C(i,p1)*C(j,p2)%MOD*va[p1][p2]%MOD*vb[i - p1][j - p2]%MOD)%MOD;
                }
            }
        }
    }
    return res;
}
table operator+(table va,table vb){
    table res = vector<vector<int>>(k1 + 1,vector<int>(k2 + 1));
    for(int i = 0;i <= k1;i++){
        for(int j = 0;j <= k2;j++){
            res[i][j] = (va[i][j] + vb[i][j])%MOD;
        }
    }
    return res;
}
pair<table,table> operator*(pair<table,table> va,pair<table,table> vb){
    return make_pair(va.first*vb.first,va.second + va.first*vb.second);
}
pair<table,table> qpow(pair<table,table> x,int p){
    pair<table,table> res = make_pair(vector<vector<int>>(k1 + 1,vector<int>(k2 + 1)),vector<vector<int>>(k1 + 1,vector<int>(k2 + 1)));
    res.first[0][0] = 1;
    while(p){
        if(p & 1){
            res = res*x;
        }
        x = x*x;
        p>>=1;
    }
    return res;
}
pair<table,table> f(long long a,long long b,long long c,long long n,pair<table,table> u,pair<table,table> r){
    if(n <= 0){
        pair<table,table> res = make_pair(vector<vector<int>>(k1 + 1,vector<int>(k2 + 1)),vector<vector<int>>(k1 + 1,vector<int>(k2 + 1)));
        res.first[0][0] = 1;
        return res;
    }
    long long m = (a*n + b)/c;
    if(m < 1){
        return qpow(r,n);
    }
    if(a > c){
        return f(a%c,b%c,c,n,u,qpow(u,a/c)*r);
    }else{
        return qpow(r,(c - b - 1)/a)*u*f(c,c - b - 1,a,m - 1,r,u)*qpow(r,n - (c*m - b - 1)/a);
    }
}
int qpow1(int x,int p){
    int res = 1;
    while(p){
        if(p & 1){
            res = 1ll*res*x%MOD;
        }
        x = 1ll*x*x%MOD;
        p>>=1;
    }
    return res;
}
void solve(){
    int l,a,b,c;
    cin>>l>>a>>b>>c>>k1>>k2;
    pair<table,table> u = make_pair(vector<vector<int>>(k1 + 1,vector<int>(k2 + 1)),vector<vector<int>>(k1 + 1,vector<int>(k2 + 1))),r = make_pair(vector<vector<int>>(k1 + 1,vector<int>(k2 + 1)),vector<vector<int>>(k1 + 1,vector<int>(k2 + 1)));
    for(int i = 0;i <= k2;i++){
        u.first[0][i] = 1;
    }
    for(int i = 0;i <= k1;i++){
        r.first[i][0] = 1;
        r.second[i][0] = 1;
    }
    pair<table,table> res = qpow(u,b/c)*f(a,b%c,c,l,u,r);
    cout<<(res.second[k1][k2] + 1ll*qpow1(0,k1)*qpow1(b/c,k2)%MOD)%MOD<<'\n';
}
int main(){
    jc[0] = 1;
    for(int i = 1;i<=10;i++){
        jc[i] = jc[i - 1]*i;
    }
    int T;
    cin>>T;
    while(T--){
        solve();
    }
}